/**
 * @(#)IBAHelper.java
 *
 *
 * @author Leon Zhang
 * @version 1.00 2010/1/20
 */

package ext.acae2.util;

import java.io.Serializable;
import java.util.Locale;
import java.util.Vector;
import java.util.Hashtable;
import java.rmi.RemoteException;
import com.ptc.core.meta.server.*;
import com.ptc.core.meta.common.*;
import wt.fc.WTObject;
import wt.fc.QueryResult;
import wt.fc.PersistenceHelper;
import wt.fc.ObjectReference;
import wt.iba.value.litevalue.AbstractValueView;
import wt.iba.value.IBAHolder;
import wt.iba.value.service.IBAValueHelper;
import wt.iba.value.DefaultAttributeContainer;
import wt.iba.value.IBAValueUtility;
import wt.iba.value.litevalue.BooleanValueDefaultView;
import wt.iba.value.litevalue.FloatValueDefaultView;
import wt.iba.value.litevalue.IntegerValueDefaultView;
import wt.iba.value.litevalue.StringValueDefaultView;
import wt.iba.value.StringValue;
import wt.iba.value.AbstractValue;
import wt.iba.value.IBAHolderReference;
import wt.util.WTException;
public class IBAHelper {
	
    public IBAHelper() {
    }
    
    // --- Attribute Section ---

   private static final String CLASSNAME = IBAHelper.class.getName();
   private static boolean VERBOSE;
   private static Locale LOCALE = Locale.CHINA;
   
   /**
    *
    * @param     object  Object to be validated
    * @return    String
    * @exception wt.util.WTException
    **/

   

   public static String getIBAStringValue( WTObject obj, String ibaName )
            throws WTException {      
        String value = null;
        String ibaClass = "wt.iba.definition.StringDefinition";
        
        try {
          if ( obj instanceof IBAHolder ) {
            IBAHolder ibaholder = (IBAHolder)obj;
            DefaultAttributeContainer defaultattributecontainer = getContainer(ibaholder);
            
            if(defaultattributecontainer != null) {
              AbstractValueView avv = getIBAValueView(defaultattributecontainer
                                                      ,ibaName,ibaClass);
              if ( avv != null ) {
                  value = ((StringValueDefaultView)avv).getValue();
              }
            }
          }
        }
        catch (RemoteException rexp) {
          rexp.printStackTrace();
        }
        return value;
        
   }
   
   //处理boolean型软属性
   public static boolean getIBABooleanValue( WTObject obj, String ibaName )
            throws WTException {

        boolean value = false;
        String ibaClass = "wt.iba.definition.BooleanDefinition";     
        try {
          if ( obj instanceof IBAHolder ) {
            IBAHolder ibaholder = (IBAHolder)obj;
            DefaultAttributeContainer defaultattributecontainer = getContainer(ibaholder);
            
            if(defaultattributecontainer != null) {
              AbstractValueView avv = getIBAValueView(defaultattributecontainer
                                                      ,ibaName,ibaClass);
              if ( avv != null ) {
                 value = (boolean)((BooleanValueDefaultView)avv).isValue();
              }
            }
          }
        }
        catch (RemoteException rexp) {
          rexp.printStackTrace();
        }
        
        return value;
        
   }
   
   public static DefaultAttributeContainer getContainer( IBAHolder ibaholder )
            throws WTException, RemoteException {
        ibaholder = IBAValueHelper.service.refreshAttributeContainer
                (ibaholder, null, LOCALE, null);
        DefaultAttributeContainer defaultattributecontainer = 
                (DefaultAttributeContainer)
                    ibaholder.getAttributeContainer();
        return defaultattributecontainer;
  }
  
   public static AbstractValueView getIBAValueView( DefaultAttributeContainer dac, String ibaName, String ibaClass )
            throws WTException {
        
        AbstractValueView aabstractvalueview[] = null;
        AbstractValueView avv = null;
        
        aabstractvalueview = dac.getAttributeValues();
        for(int j = 0; j < aabstractvalueview.length; j++) {
          String thisIBAName = aabstractvalueview[j].getDefinition().getName();
          String thisIBAValue = IBAValueUtility.getLocalizedIBAValueDisplayString
                (aabstractvalueview[j], LOCALE);
          String thisIBAClass = (aabstractvalueview[j].getDefinition()).
                      getAttributeDefinitionClassName();
          if ( thisIBAName.equals(ibaName) && thisIBAClass.equals(ibaClass) ) {
            avv=aabstractvalueview[j];
            break;
          }
        }
        return avv;
  } 
  
  
   public static String getSoftType( WTObject obj)
            throws WTException {
        String typename = "";                    
        TypeIdentifier type=TypeIdentifierUtility.getTypeIdentifier(obj);
        typename=type.getTypename();
        int nIndex1 = typename.lastIndexOf("|");
		int nIndex2 = typename.lastIndexOf(".");
		int nIndex = nIndex1 > nIndex2 ? nIndex1 : nIndex2;
		
		if (nIndex >= 0) 
		{
			typename = typename.substring(nIndex + 1);
		}
        return typename;      
   }
}